package com.mrzhou.test;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qiang.zhou
 */
@Getter
@Setter
public class BaseMenu {

    private Long parentId;

    private List<BaseMenu> children;

}

class Test {


    public static <T extends BaseMenu> List<T> baseMenuList(List<BaseMenu> list, Class<T> clazz) {
        List<T> result = new ArrayList<>();
        for(BaseMenu baseMenu: list) {
            try{
                BaseMenu t = clazz.newInstance();
                BeanUtils.copyProperties(baseMenu, t);
                if(CollectionUtils.isEmpty(list) || result.size() == list.size()) {
                    return result;
                }
                t.setChildren(baseMenuList(list, (Class<BaseMenu>) clazz));
                result.add((T) t);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static void main(String[] args) {
        List<BaseMenu> list = new ArrayList<>();
        AChildrenMenu menu = new AChildrenMenu();
        menu.setParentId(90L);
        menu.setA("AAAAA");
        list.add(menu);

        List<AChildrenMenu> menus = baseMenuList(list, AChildrenMenu.class);
    }
}
